//
//  ExchangeDiamonAlertView.swift
//  MT
//
//  Created by 陈悦 on 2020/5/15.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol ExchangeDiamonAlertViewDelegate: NSObjectProtocol {
        /// 选择的item
    func selectCell(_ model: MineWithdrawModel)
}

class ExchangeDiamonAlertView: UIView {
    
    var exchangeItems: [MineWithdrawModel] {
        didSet {
            for item in self.exchangeItems {
                item.isSelected = false
            }
        }
    }
    
    private var selectItem: MineWithdrawModel?
    
    /// 代理
    weak var delegate: ExchangeDiamonAlertViewDelegate?
    
    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var exchangeButton: UIButton!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var contentView: UIView!
    
    @IBAction func closeView(_ sender: Any) {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
        }) { (flag) in
            self.removeFromSuperview()
        }
    }
    
    @IBAction func exchangeBtnClicked(_ sender: Any) {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0
        }) { (flag) in
            self.removeFromSuperview()
        }
        if let delegate = delegate, let model = selectItem {
            delegate.selectCell(model)
        }
    }
    override init(frame: CGRect) {
        exchangeItems = []
            super.init(frame: frame)
            // 加载xib
            contentView = (Bundle.main.loadNibNamed("ExchangeDiamonAlertView", owner: self, options: nil)?.last as! UIView)
            // 设置frame
            contentView.frame = frame
            contentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            
            exchangeButton.layer.cornerRadius = 25
            // 添加上去
            addSubview(contentView)
        
                /// 设置大小
                let inset: CGFloat = 10.0
                let itemWidth = (ScreenWidth - inset * 4 - 1) / 3
                let flowLayout = UICollectionViewFlowLayout()
                flowLayout.itemSize = CGSize(width: itemWidth, height: 80)
                flowLayout.minimumLineSpacing = 10
                flowLayout.minimumInteritemSpacing = 10
        collectionView.collectionViewLayout = flowLayout
                collectionView.contentInset = UIEdgeInsets(top: 0, left: inset, bottom: inset, right: inset)
                collectionView.showsVerticalScrollIndicator = false
                collectionView.delegate = self
                collectionView.dataSource = self
        //        cv.isPagingEnabled = true
                collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .clear
                /// 注册 Cell
                collectionView.register(ExchangeItemCell.self, forCellWithReuseIdentifier: ExchangeItemCell.reuseIdentity())
    }
        
    required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
    }

}

extension ExchangeDiamonAlertView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // TODO:- Required Method
        return self.exchangeItems.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: ExchangeItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: ExchangeItemCell.reuseIdentity(), for: indexPath) as! ExchangeItemCell
        if self.exchangeItems.count > indexPath.row {
            cell.model = self.exchangeItems[indexPath.item]
        }
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        for item in self.exchangeItems {
            item.isSelected = false
        }
        let model = self.exchangeItems[indexPath.item]
        model.isSelected = true
        self.selectItem = model
        self.collectionView.reloadData()
    }

}
